package com.example.module_mine.widget

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.example.module_mine.R

/**
 * @author yaotianxue
 * @description 验证码itemView
 * @date 2025/2/8
 */
class VerifyCodeItemView: FrameLayout {
    private  var cursorView:View //光标
    private  var numberView:TextView //数字
    private  var indicatorView:View //下方指示器

    private var indicatorColor:Int //下方指示器颜色
    private var indicatorFocusedColor:Int //下方指示器选中颜色
    private var cursorColor:Int //光标颜色
    private var numberSize:Float //数字大小

    private var alphaAnimator:ValueAnimator? = null //光标动画


    constructor(context:Context):this(context,null)
    constructor(context: Context,attrs:AttributeSet?):this(context,attrs,0) //
    constructor(context: Context,attrs:AttributeSet?,defStyleAttr:Int):super(context,attrs,defStyleAttr){
        val view = LayoutInflater.from(context).inflate(R.layout.layout_verrify_code_item,this)
        cursorView = view.findViewById(R.id.view_cursor)
        numberView = view.findViewById(R.id.tv_number)
        indicatorView = view.findViewById(R.id.view_focus)
        //获得自定义属性
        val typeArray = context.obtainStyledAttributes(attrs,R.styleable.VerifyCodeItemView)
        indicatorColor = typeArray.getColor(R.styleable.VerifyCodeItemView_indicatorColor, Color.BLACK)
        indicatorFocusedColor = typeArray.getColor(R.styleable.VerifyCodeItemView_indicatorFocusedColor,Color.GRAY)
        cursorColor = typeArray.getColor(R.styleable.VerifyCodeItemView_cursorColor,Color.BLACK)
        numberSize = typeArray.getDimension(R.styleable.VerifyCodeItemView_numberSize,25f)
        typeArray.recycle()
        //设置属性
        cursorView.visibility = View.GONE
        cursorView.setBackgroundColor(cursorColor)
        numberView.paint.textSize = numberSize
        indicatorView.setBackgroundColor(indicatorColor)
    }

    /**
     * 设置数字
     */
    fun setNumber(number:String){
        numberView.text = number
    }

    /**
     * 设置焦点
     */
    fun setFocus(focus:Boolean){
        if (focus){
            onFocused()
        }else{
            onUnfocused()
        }
    }
    private fun onFocused() {
        cursorView.visibility = View.VISIBLE
        indicatorView.setBackgroundColor(indicatorFocusedColor)
        //光标动画
        alphaAnimator = ObjectAnimator.ofFloat(cursorView,"alpha",0f,1f)
        alphaAnimator?.duration = 1000
        alphaAnimator?.repeatMode = ValueAnimator.RESTART
        alphaAnimator?.repeatCount = ValueAnimator.INFINITE
        alphaAnimator?.start()

    }

    private fun onUnfocused() {
        cursorView.visibility = View.GONE
        indicatorView.setBackgroundColor(indicatorColor)
        //取消动画 防止内存泄漏
        if(alphaAnimator != null){
            alphaAnimator?.cancel()
            alphaAnimator = null
        }
    }
}